package com.taomeng.dao.impl;

import com.taomeng.dao.PermissionDao;
import com.taomeng.entity.PerDTree;
import com.taomeng.entity.Permission;
import com.taomeng.util.JdbcTemplateSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Repository
public class PermissionDaoImpl implements PermissionDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private JdbcTemplateSupport jdbcTemplateSupport;



    @Override
    public List<PerDTree> findPermissionByRoleId(String roleId) {
        String sql ="select " +
                " t_permission.id, " +
                " t_permission.`name`, " +
                " t_permission.pid " +
                "from " +
                " t_role_permission, " +
                " t_permission " +
                "where " +
                " t_role_permission.permission_id=t_permission.id " +
                " and " +
                " t_role_permission.role_id=? ";
        Object[] params = {roleId};
        try {
            return jdbcTemplate.query(sql, params, (resultSet, i) -> {
                PerDTree perDTree = new PerDTree();
                perDTree.setId(String.valueOf(resultSet.getLong("id")));
                perDTree.setTitle(resultSet.getString("name"));
                perDTree.setCheckArr("1");
                perDTree.setParentId(String.valueOf(resultSet.getLong("pid")));
                return perDTree;
            });
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<PerDTree> findAllPermission() {
        String sql = "select id,name,pid from t_permission";
        try {
            return jdbcTemplate.query(sql,(resultSet, i) -> {
                PerDTree perDTree = new PerDTree();
                perDTree.setId(String.valueOf(resultSet.getLong("id")));
                perDTree.setTitle(resultSet.getString("name"));
                perDTree.setCheckArr("0");
                perDTree.setParentId(String.valueOf(resultSet.getLong("pid")));
                return perDTree;
            });
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void delPermByRoleId(String roleId, String per) {
        String sql = "delete from t_role_permission where role_id = ? and permission_id = ?";
        Object params[] = {
                roleId,
                per
        };
        try {
            jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int[] batchAddPermission(String roleId, List<String> list) {
        String sql = "insert into t_role_permission values ( ? , ? )";
        //批量插入权限
        try {
            return jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ps.setBigDecimal(1, BigDecimal.valueOf(Integer.parseInt(roleId)));
                    ps.setBigDecimal(2, BigDecimal.valueOf(Integer.parseInt(list.get(i))));
                }

                @Override
                public int getBatchSize() {
                    return list.size();
                }
            });
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean delAllPermByRoleId(Long id) {
        String sql = "delete from t_role_permission where role_id = ?";
        Object[] params = {id};
        try {
            return jdbcTemplate.update(sql,params)>=0;
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return false;
    }
}